﻿package core.ui.util
{
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class CommandInput extends FloatBox
    {
        var ed:Tuyitu;
        var callback:Function;
        var drag:Sprite;
        var text:TextInput;
        var ok:Button;
        var cancel:Button;

        public function CommandInput(param1:Tuyitu, param2:String, param3:String, param4:Function)
        {
            var ed:*;
            var _arg1:* = param1;
            var _arg2:* = param2;
            var _arg3:* = param3;
            var _arg4:* = param4;
            ed = _arg1;
            var rm_text:* = _arg2;
            var val:* = _arg3;
            var callback:* = _arg4;
            super(240, 130);
            this.ed = ed;
            this.callback = callback;
            ed.addChild(this);
            AppState.TextInteraction = true;
            this.drag = new Sprite();
            this.drag.addEventListener(MouseEvent.MOUSE_DOWN, this.DragMouseDown, false, 0, true);
            this.drag.graphics.beginFill(9474192);
            this.drag.graphics.drawRect(0, 0, 240, 20);
            this.drag.graphics.endFill();
            this.drag.alpha = 0;
            addChild(this.drag);
            var tx:* = new Literal(rm_text);
            tx.x = 8;
            tx.y = 15;
            addChild(tx);
            this.text = new TextInput(219, 100, val);
            this.text.x = 10;
            this.text.y = 33;
            addChild(this.text);
            this.text.t.setSelection(0, this.text.t.length);
            stage.focus = this.text.t;
            this.ok = new Button("rm_ok", 70);
            this.ok.addEventListener(MouseEvent.CLICK, this.OK, false, 0, true);
            this.ok.x = this.width - 82;
            this.ok.y = this.height - 33;
            addChild(this.ok);
            this.cancel = new Button("rm_cancel", 70);
            this.cancel.addEventListener(MouseEvent.CLICK, this.Close, false, 0, true);
            this.cancel.x = this.width - 162;
            this.cancel.y = this.height - 33;
            addChild(this.cancel);
            ed.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false, 1, true);
            setTimeout(function (... args)
            {
                ed.stage.addEventListener(MouseEvent.CLICK, ClickOutside, false, 1, true);
                return;
            }// end function
            , 200);
            this.x = int(stage.stageWidth / 2 - 120);
            this.y = int(stage.stageHeight / 2 - 75);
            return;
        }// end function

        public function ClickOutside(... args) : void
        {
            if (!this.hitTestPoint(this.ed.stage.mouseX, this.ed.stage.mouseY))
            {
                this.Close();
            }
            return;
        }// end function

        public function Close(... args) : void
        {
            AppState.TextInteraction = false;
            this.ed.stage.removeEventListener(MouseEvent.CLICK, this.ClickOutside, false);
            this.ed.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false);
            this.ed.removeChild(this);
            return;
        }// end function

        private function OK(... args) : void
        {
            this.Close();
            this.callback(this.text.value);
            return;
        }// end function

        private function KeyDown(event:KeyboardEvent) : void
        {
            if (event.keyCode == 13)
            {
                this.OK();
            }
            else if (event.keyCode == 27)
            {
                this.Close();
            }
            return;
        }// end function

        function DragMouseDown(event:MouseEvent) : void
        {
            this.startDrag(false, new Rectangle((-this.width) / 2, 40, stage.stageWidth, stage.stageHeight + 1000));
            this.ed.stage.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, true, 0, true);
            return;
        }// end function

        function DragMouseUp(event:MouseEvent) : void
        {
            this.stopDrag();
            this.ed.stage.removeEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, true);
            return;
        }// end function

    }
}
